//
//  CQTemplateCell.m
//  CQSplicedTemplateKit
//
//  Created by ciyouzen on 2021/3/16.
//

#import "CQTemplateCell.h"
#import "UIImageView+CQSplicedTemplateKit.h"

@implementation CQTemplateCell

/*
 *  初始化
 *
 *  @param showDemoAnimate      是否布局结束就显示缩放的演示动画
 *
 *  @return 模板内部中的模板块视图
 */
- (instancetype)initWithShowDemoAnimate:(BOOL)showDemoAnimate {
    self = [super initWithContentMode:UIViewContentModeScaleAspectFill];
    if (self) {
        self.pinchMaxScale = 4;
        self.controlMaxScaleOnPinchChanging = YES;
        
        if (showDemoAnimate) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self showDemoAnimate];
            });
            
        }
    }
    return self;
}

#pragma mark - Update
- (void)updateImageByDataModel:(id)dataModel {
    if ([dataModel isKindOfClass:[UIImage class]]) {
        UIImage *image = (UIImage *)dataModel;
        [[self imageView] setImage:image];
        
        //_hasSetFrameForThisImage = NO;
        [self updateFrameByImage:image];
        
    } else if ([dataModel isKindOfClass:[NSString class]]) {
        NSString *imageUrl = (NSString *)dataModel;
        [[self imageView] CQSplicedTemplateKit_setImageWithUrl:imageUrl placeholderImage:nil completed:^(UIImage * _Nullable bResultImage, NSURL * _Nullable imageURL) {
            //_hasSetFrameForThisImage = NO;
            [self updateFrameByImage:bResultImage];
        }];
    }
}



/// 显示演示的动画
- (void)showDemoAnimate {
//    self.layer.borderColor = [UIColor whiteColor].CGColor;
//    self.layer.borderWidth = 0.5;
    
    [self enlargeToScale:1.3 animateWithDuration:0.6 completion:^(BOOL finished) {
        [self enlargeToScale:1.0 animateWithDuration:0.6 completion:nil];
    }];
}





// 添加曲线：已经在updateTemplateWithRealTemplateModel中执行过，所以以下不再重复执行
/*
#pragma mark - DrawRect
- (void)drawRect:(CGRect)rect { // 请在设置完realTemplateModel后，执行 setNeedsDisplay
    [super drawRect:rect];
    
    if (self.realPointMaskLayer == nil) {
        [self addMaskLayer];
    }
    
    UIBezierPath *path = self.realTemplateModel.realTemplatePointPath;
    self.realPointMaskLayer.path = path.CGPath;
}


- (void)addMaskLayer {
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    //maskLayer.path = path.CGPath;
    maskLayer.fillColor = [[UIColor whiteColor] CGColor];
//    maskLayer.frame = _imageview.frame;
    self.layer.mask = maskLayer;
    _realPointMaskLayer = maskLayer;
}
*/

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
